package org.raymond.iworks.apis.common;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

import java.util.stream.Collectors;

public class BaseController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;

    protected String toFieldErrorString(BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return bindingResult.getFieldErrors().stream().map(error->
                            String.format("\n%s %s.", error.getField(), error.getDefaultMessage()))
                    .collect(Collectors.joining(""));
        }
        return "";
    }
}
